package it.bela.pgcdc.editor;

import it.bela.pgcdc.domain.Hobby;
import it.bela.pgcdc.service.HobbyService;

import java.beans.PropertyEditorSupport;

public class HobbyPropertyEditor extends PropertyEditorSupport {

	private HobbyService hobbyService;
 
	public HobbyPropertyEditor(HobbyService hobbyService) {
		this.hobbyService = hobbyService;
	}
	
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
    	Long id = null;
    	Hobby hobby = null;
    	if (!text.equals("-")){
    		id = new Long(text);
    		hobby = hobbyService.findById(Hobby.class, id);
    	}
        super.setValue(hobby);
    }
    
    @Override
    public String getAsText() {
    	Hobby hobby = (Hobby) getValue();
    	String text = null;
    	if (hobby != null)
    		text = hobby.getId().toString();
    	return text;
    }
	
}
